package net.lab1024.sa.admin.module.business.exam.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.lab1024.sa.admin.module.business.exam.dao.CourseaDao;
import net.lab1024.sa.admin.module.business.exam.dao.CoursebDao;
import net.lab1024.sa.admin.module.business.exam.dao.CoursecDao;
import net.lab1024.sa.admin.module.business.exam.domain.entity.*;
import net.lab1024.sa.admin.module.business.exam.domain.form.add.CoursebAddForm;
import net.lab1024.sa.admin.module.business.exam.domain.form.query.CoursebQueryForm;
import net.lab1024.sa.admin.module.business.exam.domain.form.update.CoursebUpdateForm;
import net.lab1024.sa.admin.module.business.exam.domain.vo.CoursebVO;
import net.lab1024.sa.admin.module.system.employee.dao.EmployeeDao;
import net.lab1024.sa.admin.module.system.employee.domain.entity.EmployeeEntity;
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import net.lab1024.sa.base.common.util.SmartRequestUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 生产订单与机器对应表 Service
 *
 * @Author AKITA
 * @Date 2025-05-22 15:32:44
 * @Copyright AKITA
 */

@Service
public class CoursebService {

    @Resource
    private CoursebDao coursebDao;
    @Resource
    private CoursecDao coursecDao;
    @Autowired
    private EmployeeDao employeeDao;
    @Autowired
    private CourseaDao courseaDao;

    /**
     * 分页查询
     */
    public PageResult<CoursebVO> queryPage(CoursebQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<CoursebVO> list = coursebDao.queryPage(page, queryForm);
        return SmartPageUtil.convert2PageResult(page, list);
    }

    /**
     * 添加
     */
    public ResponseDTO<String> add(CoursebAddForm addForm) {
        CoursebEntity coursebEntity = SmartBeanUtil.copy(addForm, CoursebEntity.class);
        coursebDao.insert(coursebEntity);
        return ResponseDTO.ok();
    }

    /**
     * 更新
     *
     */


    @Transactional
    public ResponseDTO<String> update(CoursebUpdateForm updateForm) {




        /*
         * 1、判断主表的状态
         *
         *
         *
         *
         * */
        Long requestUserId = SmartRequestUtil.getRequestUserId();
        CoursecEntity coursecEntity = coursecDao.selectOne(new LambdaQueryWrapper<CoursecEntity>().eq(CoursecEntity::getDocEntry, updateForm.getDocEntry()).eq(CoursecEntity::getUserId, requestUserId));
        if (!coursecEntity.getLineStatus().equals("C")){
            if (updateForm.getUpdateType().equals('A')) {
                if ("O".equals(coursecEntity.getLineStatus())) {
                    coursecEntity.setBeginDate(LocalDateTime.now());
                    coursecEntity.setLineStatus("L");
                    coursecDao.update(coursecEntity, new LambdaQueryWrapper<CoursecEntity>().eq(CoursecEntity::getDocEntry, updateForm.getDocEntry()).eq(CoursecEntity::getUserId, requestUserId));
                }
                CoursebEntity coursebEntity = coursebDao.selectOne(new LambdaQueryWrapper<CoursebEntity>().eq(CoursebEntity::getDocEntry, updateForm.getDocEntry()).eq(CoursebEntity::getLineNum, updateForm.getLineNum()));
                if (coursebEntity.getLineStatus().equals("O")) {
                    coursebEntity.setBeginDate(LocalDateTime.now());
                    coursebEntity.setLineStatus("L");
                    coursebDao.update(coursebEntity, new LambdaQueryWrapper<CoursebEntity>().eq(CoursebEntity::getDocEntry, updateForm.getDocEntry()).eq(CoursebEntity::getLineNum, updateForm.getLineNum()));
                }
            } else {
                CoursebEntity coursebEntity = coursebDao.selectOne(new LambdaQueryWrapper<CoursebEntity>().eq(CoursebEntity::getDocEntry, updateForm.getDocEntry()).eq(CoursebEntity::getLineNum, updateForm.getLineNum()));
                if (coursebEntity.getLineStatus().equals("L")) {
                    coursebEntity.setEndDate(LocalDateTime.now());
                    coursebEntity.setLineStatus("C");
                    coursebDao.update(coursebEntity, new LambdaQueryWrapper<CoursebEntity>().eq(CoursebEntity::getDocEntry, updateForm.getDocEntry()).eq(CoursebEntity::getLineNum, updateForm.getLineNum()));
                }
                List<CoursebEntity> coursebEntities = coursebDao.selectList(new LambdaQueryWrapper<CoursebEntity>().eq(CoursebEntity::getDocEntry, updateForm.getDocEntry()).eq(CoursebEntity::getUserId, requestUserId).eq(CoursebEntity::getLineStatus, 'O').or().eq(CoursebEntity::getLineStatus, 'L'));
                if (coursebEntities.isEmpty()) {
                    coursecEntity.setEndDate(LocalDateTime.now());
                    coursecEntity.setLineStatus("C");
                    coursecDao.update(coursecEntity, new LambdaQueryWrapper<CoursecEntity>().eq(CoursecEntity::getDocEntry, updateForm.getDocEntry()).eq(CoursecEntity::getUserId, requestUserId));
                }
            }

        }









        return ResponseDTO.ok();
    }

    /**
     * 批量删除
     */
    public ResponseDTO<String> batchDelete(List<Long> idList) {
        if (CollectionUtils.isEmpty(idList)){
            return ResponseDTO.ok();
        }

        coursebDao.deleteBatchIds(idList);
        return ResponseDTO.ok();
    }

    /**
     * 单个删除
     */
    public ResponseDTO<String> delete(Long docEntry) {
        coursebDao.deleteById(docEntry);
        coursecDao.deleteById(docEntry);
        return ResponseDTO.ok();
    }


    @Transactional
    public ResponseDTO<String> batchAdd(List<Long> addForms, Long docEntry) {
        List<EmployeeEntity> employeeEntities = employeeDao.selectList(new LambdaQueryWrapper<EmployeeEntity>().in(EmployeeEntity::getEmployeeId, addForms));
        CoursebEntity coursebEntity = new CoursebEntity();
        CoursecEntity coursecEntity = new CoursecEntity();

        Integer i = coursebDao.selectLineNum(docEntry);

        Integer j = coursecDao.selectLineNum(docEntry);

        if (i == null){
            i = 0;
        }
        if (j == null){
            j = 0;
        }
        List<CourseaEntity> courseaEntities = courseaDao.selectList(new LambdaQueryWrapper<CourseaEntity>().eq(CourseaEntity::getDocEntry, docEntry));

        for (EmployeeEntity employeeEntity : employeeEntities) {
            coursecEntity.setDocEntry(docEntry);
            coursecEntity.setLineNum(++j);
            coursecEntity.setUserId(employeeEntity.getEmployeeId());
            coursecEntity.setUserName(employeeEntity.getLoginName());
            coursecEntity.setLineStatus("O");
            coursecDao.insert(coursecEntity);
            for (CourseaEntity courseaEntity : courseaEntities) {
                coursebEntity.setDocEntry(docEntry);
                coursebEntity.setLineNum(++i);
                coursebEntity.setBaseEntry(docEntry);
                coursebEntity.setBaseLineNum(courseaEntity.getLineNum());
                coursebEntity.setTitle(courseaEntity.getTitle());
                coursebEntity.setFile(courseaEntity.getFile());
                coursebEntity.setType(courseaEntity.getType());
                coursebEntity.setUserId(employeeEntity.getEmployeeId());
                coursebEntity.setUserName(employeeEntity.getLoginName());
                coursebEntity.setLineStatus("O");
                coursebDao.insert(coursebEntity);
            }
        }

        return ResponseDTO.ok();


    }
}
